package com.usian.user.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.usian.common.admin.AdminConstans;
import com.usian.model.article.pojos.ApAuthor;
import com.usian.model.base.BaseServiceImpl;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.media.pojos.WmUser;
import com.usian.model.user.dtos.AuthDto;
import com.usian.model.user.pojos.ApUser;
import com.usian.model.user.pojos.ApUserRealname;
import com.usian.user.feign.ArticleFeign;
import com.usian.user.feign.WemediaFeign;
import com.usian.user.mapper.ApUserMapper;
import com.usian.user.mapper.ApUserRealnameMapper;
import com.usian.user.service.ApUserRealnameService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.LinkedHashMap;

@Service
public class ApUserRealnameServiceImpl extends BaseServiceImpl<ApUserRealnameMapper, ApUserRealname> implements ApUserRealnameService {

    @Autowired
    private ArticleFeign articleFeign;

    @Autowired
    private WemediaFeign wemediaFeign;

    @Autowired
    private ApUserMapper apUserMapper;

    @Override
    public ResponseResult loadListByStatus(AuthDto dto) {
        if (dto == null) return errorParam;
        return getPageData(dto, Wrappers.<ApUserRealname>lambdaQuery().eq(dto.getStatus() != null, ApUserRealname::getStatus, dto.getStatus()));
    }

    @GlobalTransactional
    @Override
    public ResponseResult updateStatusById(AuthDto dto, Short status) {
        //1.检查参数
        if (dto == null || dto.getId() == null) return errorParam;

        //通过传过来的 id和状态 对认证信息进行修改
        ApUserRealname realname = new ApUserRealname();
        realname.setId(dto.getId());
        realname.setStatus(status);
        if (StringUtils.isNotEmpty(dto.getMsg()))realname.setReason(dto.getMsg());
        updateById(realname);

        //添加自媒体表和作者表
        //根据dto的认证id查询到认证信息,再根据用户id获取到用户信息
        ApUserRealname apUserRealname = getById(dto.getId());
        ApUser user_c = apUserMapper.selectById(apUserRealname.getId());
        if (status == AdminConstans.PASS_AUTH) {
            //先根据用户名字查询是否存在数据,存储数据
            WmUser wmUser = wemediaFeign.findByName(user_c.getName());
            if (wmUser == null) {
                wmUser = new WmUser();
                wmUser.setApUserId(dto.getId());
                BeanUtils.copyProperties(user_c, wmUser);
                wmUser.setCreatedTime(new Date());
                wmUser.setStatus(9);
                wmUser.setType(0);
                ResponseResult save = wemediaFeign.save(wmUser);
                LinkedHashMap map = (LinkedHashMap) save.getData();
                wmUser.setId((Integer) map.get("id"));
            }

            //添加作者表,先查询是否存在数据
            ApAuthor author = articleFeign.findByUserId(wmUser.getId());
            if (author == null) {
                ApAuthor apAuthor = new ApAuthor();
                apAuthor.setName(user_c.getName());
                apAuthor.setType(2);
                apAuthor.setCreatedTime(new Date());
                apAuthor.setUserId(user_c.getId());
                apAuthor.setWmUserId(wmUser.getId());
                ResponseResult save = articleFeign.save(apAuthor);
                LinkedHashMap map = (LinkedHashMap) save.getData();
                wmUser.setApAuthorId((Integer) map.get("id"));
                wemediaFeign.updateAuthorId(wmUser);
            }

            //最后把c端用户的id状态修改
            ApUser tempUser = new ApUser();
            tempUser.setId(user_c.getId());
            tempUser.setFlag((short) 1);
            apUserMapper.updateById(tempUser);
        }
        return success;
    }
}
